package glossos.parsing.fsm;

import glossos.parsing.CharMatcher;
import glossos.parsing.action.NullAction;
import java.util.HashSet;
import java.util.Set;

public class State {
	private final Set<Transition> transitions;
	private final Transition nullTransition = new Transition(CharMatcher.ANY, new NullAction(), this);
	private String name;

	public State(String name){
        this();
        this.name = name;
	}
	
	public State() {
		transitions = new HashSet<Transition>();
	}

	public State addTransition(Transition transition){
		transitions.add(transition);
		return this;
	}

	public State next(char c){
		for (Transition transition : transitions) {
            if (transition.isDefinedFor(c))
				return transition.trigger(c);
		}
		return nullTransition.trigger(c);
    }

	@Override
	public String toString() {
		return name;
	}
}
